package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 角色管理
 */
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    public RoleService roleService;

    //查询全部角色
    @RequestMapping("/findAll")
    public Result findAll() {
        List<Role> checkItems = roleService.findAll();
        if (checkItems != null && checkItems.size() > 0) {
            Result result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
            result.setData(checkItems);
            return result;
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    /*//根据id查询角色信息
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Role role = roleService.findById(id);
        if (role != null) {
            Result result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            result.setData(role);
            return result;
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    @RequestMapping("/findRoleIdsByUserId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try{
            List<Integer> roleIds = roleService.findRoleIdsByUserId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,roleIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    }*/

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.pageQuery(queryPageBean);
    }


    @RequestMapping("/findMaxAll")
    public Result findMaxAll(){
        try {
            Map<String, List> maxAll = roleService.findMaxAll();
            return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS,maxAll);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] PermissionIds,Integer[] MenuIds) {
        try {
            roleService.add(role, PermissionIds,MenuIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
    }

    @RequestMapping("/findByIds")
    public Result findById(Integer id){

        try {
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS,role);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }

    }

    @RequestMapping("/findPermissionByRole")
    public Result findPermissionByRole(Integer id){
        try{
            List<Integer> permissionIds =
                    roleService.findPermissionByRole(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,permissionIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try{
            List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,menuIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] PermissionIds,Integer[] MenuIds) {
        try {
            roleService.edit(role, PermissionIds,MenuIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
    }
}

